package cn.thoughtworks.school.usercenter.controllers;

import cn.thoughtworks.school.usercenter.services.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/organizations")
public class OrganizationController {
  @Autowired
  private OrganizationService organizationService;

  @GetMapping("{organizationId}/users/roles")
  public ResponseEntity findRolesByOrganizationId(@PathVariable("organizationId") Long organizationId) {
    return ResponseEntity.ok(organizationService.findRolesByOrganizationId(organizationId));
  }

  @GetMapping("{organizationId}/administrators")
  public ResponseEntity getAdminsRoleByOrganizationId(@PathVariable("organizationId") Long organizationId){
    List users = organizationService.getAdminsRoleByOrganizationId(organizationId);
    return ResponseEntity.ok(users);
  }

  @PostMapping(value = "{organizationId}/users/searches")
  ResponseEntity searchUsersByConditions(@PathVariable Long organizationId,
                                         @RequestBody Map<String, Object> searchParam) {
    List<Map> result = organizationService.getUsersByOrganizationIdAndConditions(organizationId, searchParam);

    return new ResponseEntity(result, HttpStatus.CREATED);
  }

  @GetMapping(value = "{organizationId}/users/ids/{ids}")
  public ResponseEntity getUsersByOrganizationIdAndIds(@PathVariable Long organizationId,
                                                       @PathVariable String ids) {
    List<Map> results = organizationService.getUsersByOrganizationIdAndIds(organizationId, ids);

    return new ResponseEntity<>(results, HttpStatus.OK);
  }

  @DeleteMapping(value = "{organizationId}/users/{userId}/roles")
  public ResponseEntity deleteOrganizationUserRole(@PathVariable Long organizationId,
                                                   @PathVariable Long userId,
                                                   @RequestParam Integer roleNumber) {
    organizationService.deleteOrganizationUserRole(organizationId, userId,roleNumber);

    return ResponseEntity.noContent().build();
  }

}
